<?php
class Login extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->database = $this->_loader->database(); 
        $this->session = $this->_loader->library('Session');   
    }

    public function actionIndex()
    {
        if($this->_session->get('adminid'))
        {
            return $this->responseRedirect('home');
        }
        else
        {
            $AdminModel = $this->_loader->model('AdminModel');
            $AdminInput = $this->_input->filter(array(
                'username' => Input::STRING,
                'password' => Input::STRING
                ));
            if(!$AdminInput['username'] && !$AdminInput['password'])
            {
                return $this->responseView('login/login');
            }
            else if($adminid = $AdminModel->login($AdminInput['username'],$AdminInput['password']))
            {
                $this->_session->set('adminid',$adminid);
                return $this->responseRedirect('home');
            }
            else
            {
                $data['error'] = 'Đăng nhập thất bại';
                return $this->responseView('login/login', $data);
            }

        }
    }
    public function actionLogout()
    {
        if($this->_session->get('adminid'))
        {
            $this->_session->destroy('adminid');
            return $this->responseRedirect('login');
        }
        else
        {
            return $this->responseView('login/login');
        }
    }
}